package cc.rengu.redp.bizimpl.monitor.service.impl;

import cc.rengu.igas.bomp.facade.BompMchntPublicDealService;
import cc.rengu.igas.bomp.facade.request.MchntBaseInfoQueryRequest;
import cc.rengu.igas.bomp.facade.request.ReloadCacheRequest;
import cc.rengu.igas.bomp.facade.response.ReloadCacheResponse;
import cc.rengu.igas.bomp.facade.result.Result;
import cc.rengu.redp.bizimpl.enums.BompTxnNumEnum;
import cc.rengu.redp.bizimpl.monitor.service.ReloadCacheService;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpBMchntBaseInfoService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpSysParamService;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ReloadCacheServiceImpl implements ReloadCacheService {

    @Autowired
    private IUpmpSysParamService upmpSysParamService;

    @Autowired
    private IUpmpBMchntBaseInfoService bMchntBaseInfoService;

    @DubboReference(version = "${dubbo.protocol.version}", check = false)
    private BompMchntPublicDealService mhntPublicDealService;


    @Override
    public OperationResult reloadCache(String instId, String cacheType) {
        UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        ReloadCacheRequest req = new ReloadCacheRequest();
        req.setHeader(CommonUtils.packageBompHeader(bMchntBaseInfoService.genMerOrder(instId), BompTxnNumEnum.reloadCache.getType(), instId, null));
        req.setCacheType(cacheType);
        req.setUserId(user.getId());
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();

        try {
            log.info("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易请求报文：" + JSON.toJSONString(req));
            Result<ReloadCacheResponse> response = mhntPublicDealService.reloadCache(req);
            log.info("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易返回报文：" + JSON.toJSONString(response));
            if (response.isSuccess()) {
                ReloadCacheResponse resp = response.getResult();
                result.put("respCode", resp.getRespCode());
                result.put("respMsg", resp.getRespDesc());
                // 返回码：00-进件成功，其他均视为失败
                if (BusinessContainer.BSPS_SUCCESS.equals(resp.getRespCode())) {

                    log.info("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易成功");
                    return OperationResult.buildSuccessResult(BompTxnNumEnum.reloadCache.getDesc() + "交易成功", result);
                } else {
                    log.error("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易失败");
                    return OperationResult.buildFailureResult(BompTxnNumEnum.reloadCache.getDesc() + "交易失败", result);
                }
            } else {
                result.put("resCode", BusinessContainer.ERROR);
                result.put("resMsg", BompTxnNumEnum.reloadCache.getDesc() + "没有获得返回报文");
                log.error("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易没有获得返回信息");
                return OperationResult.buildFailureResult(BompTxnNumEnum.reloadCache.getDesc() + "交易失败", result);
            }
        } catch (Exception e) {
            // TODO: handle exception
            result.put("resCode", BusinessContainer.ERROR);
            result.put("resMsg", e.getMessage());
            log.error("法人行[" + instId + "],用户标识[" + req.getUserId() + "]" + BompTxnNumEnum.reloadCache.getDesc() + "交易出现异常，异常信息：" + e.getMessage(), e);
            return OperationResult.buildFailureResult(BompTxnNumEnum.reloadCache.getDesc() + "交易失败", result);
        }
    }
}
